#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget),
      hotkey_1(new QHotkey(this)),
      hotkey_2(new QHotkey(this))
{
    ui->setupUi(this);


#ifdef TEST_MAPPING
    //shortcut mapping override
    QHotkey::addGlobalMapping(QKeySequence("X"), QHotkey::NativeShortcut());// add invalid mapping to test if the overwrite works for all platforms
#endif



    //1
    connect(this->ui->hotkeyCheckbox_1, &QCheckBox::toggled,
            this->hotkey_1, &QHotkey::setRegistered);
    connect(this->ui->hotkeySequenceEdit_1, &QKeySequenceEdit::keySequenceChanged,
            this, &Widget::setShortcut_1);
    connect(this->hotkey_1, &QHotkey::activated,
            this, [&](){
                getPos(1);
            });

    //2
    connect(this->ui->hotkeyCheckbox_2, &QCheckBox::toggled,
            this->hotkey_2, &QHotkey::setRegistered);
    connect(this->ui->hotkeySequenceEdit_2, &QKeySequenceEdit::keySequenceChanged,
            this, &Widget::setShortcut_2);
    connect(this->hotkey_2, &QHotkey::activated,
            this, [&](){
        getPos(2);
    });

}

Widget::~Widget()
{
    delete ui;
}

void Widget::Print(){

}

void Widget::getPos(int str){
    bool status =ui->checkBox->isChecked();
    if (str == 1){
        if (status == true){
            mousePos.setMapPosA();
            POINT pos = mousePos.getMapPosA();
            QString str = QString::number(pos.x) + "," + QString::number(pos.y);
            ui->lineEdit_5->setText(str);
        }else{
            mousePos.setMyselfPos();
            POINT pos = mousePos.getMyselfPos();
            QString str = QString::number(pos.x) + "," + QString::number(pos.y);
            ui->lineEdit->setText(str);
        }
    }else if (str == 2){
        if (status == true){
            mousePos.setMapPosB();
            POINT pos = mousePos.getMapPosB();
            QString str = QString::number(pos.x) + "," + QString::number(pos.y);
            ui->lineEdit_6->setText(str);
        }else{
            mousePos.setEnemyPos();
            POINT pos = mousePos.getEnemyPos();
            QString str = QString::number(pos.x) + "," + QString::number(pos.y);
            ui->lineEdit_2->setText(str);
        }


    }

}

void Widget::setShortcut_1(const QKeySequence &sequence)
{
    this->hotkey_1->setShortcut(sequence, false);
}

void Widget::setShortcut_2(const QKeySequence &sequence)
{
    this->hotkey_2->setShortcut(sequence, false);
}


void Widget::on_pushButton_3_clicked()
{
//    double scale = mousePos.getScale();
//    if (scale == 0){
        double len = ui->lineEdit_3->text().toDouble();
        mousePos.setScale(len);
        double scale = mousePos.getScale();
//    }
    int dis = mousePos.CountDis(scale);
    QString dis_str = QString::number(dis);
    ui->lineEdit_4->setText(dis_str);


}

